package com.ruoyi.common.log.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.log.domain.City;
import com.ruoyi.common.log.domain.Crop;
import com.ruoyi.common.log.domain.Factory;
import com.ruoyi.common.log.service.CityService;
import com.ruoyi.common.log.service.CropService;
import com.ruoyi.common.log.service.FactoryService;
import com.ruoyi.common.log.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("factory")
public class FactoryController {

    @Autowired
    FactoryService factoryService;

    @Autowired
    CityService cityService;

    @Autowired
    CropService cropService;

    @RequestMapping("factoryList")
    public Result factoryList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, @RequestBody Factory factory){
        PageHelper.startPage(pageNum,pageSize);
        List<Factory> list = factoryService.factoryList(factory);
        PageInfo<Factory> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @RequestMapping("cityList")
    public Result cityList(){
        List<City> list = cityService.list();
        //配置'
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(list, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());
                });
        return Result.success(treeNodes);
    }

    @RequestMapping("factoryAdd")
    public Result factoryAdd(@RequestBody Factory factory){
        factory.setFactoryCreationTime(new Date());
        boolean save = factoryService.saveOrUpdate(factory);
        return save?Result.success():Result.fail();
    }

    @RequestMapping("cropList")
    public Result cropList(){
        List<Crop> list = cropService.list();
        return Result.success(list);
    }

    @RequestMapping("getSelect")
    public Result getSelect(Integer factoryId){
        List<Factory> getSelect=factoryService.getSelect(factoryId);
        return Result.success(getSelect);
    }

    @RequestMapping("factoryDelete")
    public Result factoryDelete(@RequestBody Integer factoryId){
        boolean b = factoryService.removeById(factoryId);
        return b?Result.success():Result.fail();
    }

}
